#include <string>
#include"HiKernel/HiModule.h"
#include"HiKernel/HiViewer.h"
#include"HiKernel/HiRenderer.h"

#include <windows.h>
#include <Winbase.h>

HiModule::HiModule()
{
	m_bActive = false;
}
        
HiModule* 
HiModule::Load( const std::string& _className, const std::string& _dsoName, HiXmlNode* _xml  )
{
	std::string strname;
	
    if ( _dsoName =="") 
	{
#if defined(WIN32)
    #ifdef _DEBUG
		strname = _className+"_d.dll";
    #else
		strname = className +".dll"
    #endif
#endif
     } 
	else 
	{
		strname = _dsoName;
	}

	HINSTANCE instance = LoadLibrary(( LPCTSTR )strname.c_str());

    if ( instance == NULL ) 
    {
		printf("module %s is NULL\n", _className.c_str());
        return 0;
	}

    std::string strProcess = "HiLoadModule_" + _className;

	HiModule* (*pCreateModuleFunc)(HiXmlNode*) = (HiModule*(*)(HiXmlNode*))GetProcAddress(instance, strProcess.c_str() );

    if ( pCreateModuleFunc== NULL ) 
		return 0;
	else 
		return pCreateModuleFunc(_xml);
}

int 
HiModule::ParseXml(HiXmlNode* _xmlRoot)
{
    if(_xmlRoot==NULL)
            return 0;
    
    SetXmlAttrs(_xmlRoot);

	int i, n = _xmlRoot->GetNumChildren();
    for(i=0;i<n;i++)  {
		HiXmlNode* xml = _xmlRoot->GetChild(i);
            SetXmlAttrs(xml);
    }


    return 1;
}
//
//bool HiModule::HandleMessage(const Telegram &msg)
//{
//	return true;
//}

void HiModule::SetActive(bool bActive)
{
	m_bActive = bActive;
}
